﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace KLADR
{
    public struct addressItem
    {
        public string name;
        public string socr;
        public string code;
        public kladrDataSet ds;
    }

    class AccessKladr
    {
        public AccessKladr()
        {
            SqlConnectionString = Properties.Settings.Default.kladrConnectionString;
        }

        public string SqlConnectionString { get; set; }


        private addressItem FindAddressTemplate(kladrDataSet ds)
        {
            addressItem result = new addressItem { name = "", code = "", socr = "", ds = ds };
            try
            {
                if (ds.kladr.Rows.Count == 1)
                {
                    kladrDataSet.kladrRow row = (ds.kladr.Rows[0] as kladrDataSet.kladrRow);
                    result.name = row.name.Trim();
                    result.socr = row.socr.Trim().ToUpper();
                    result.code = row.code;
                    return result;
                }
                if (ds.street.Rows.Count == 1)
                {
                    kladrDataSet.streetRow row = (ds.street.Rows[0] as kladrDataSet.streetRow);
                    result.name = row.name.Trim();
                    result.socr = row.socr.Trim().ToUpper();
                    result.code = row.code;
                    return result;
                }
            }
            catch (Exception)
            {
            }
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="regionName"></param>
        /// <param name="recursy"></param>
        /// <returns></returns>
        private kladrDataSet FindRegion(string regionName)
        {
            kladrDataSet ds = new kladrDataSet();
            try
            {
                regionName = new StringBuilder(regionName.Trim().ToUpper()).Append("%").ToString();
                kladrDataSetTableAdapters.kladrTableAdapter kladr = new kladrDataSetTableAdapters.kladrTableAdapter();
                kladr.Connection = new SqlConnection(SqlConnectionString);
                kladr.FillRegionByName(ds.kladr, regionName);
            }
            catch (Exception)
            {
            }
            return ds;
        }

        public addressItem FindRegionAddress(string regionName)
        {
            kladrDataSet ds = FindRegion(regionName);
            addressItem result = FindAddressTemplate(ds);
            return result;
        }

        private kladrDataSet FindDistrict(string regionCode, string districtName)
        {
            kladrDataSet ds = new kladrDataSet();
            try
            {
                districtName = new StringBuilder(districtName.Trim().ToUpper()).Append("%").ToString();
                kladrDataSetTableAdapters.kladrTableAdapter kladr = new kladrDataSetTableAdapters.kladrTableAdapter();
                kladr.Connection = new SqlConnection(SqlConnectionString);
                kladr.FillDistrictByRegionAndName(ds.kladr, regionCode, districtName); 
            }
            catch (Exception)
            {
            }
            return ds;
        }

        public addressItem FindDistrictAddress(string regionCode, string districtName)
        {
            kladrDataSet ds = FindDistrict(regionCode, districtName);
            addressItem result = FindAddressTemplate(ds);
            return result;
        }

        private kladrDataSet FindTown(string regionCode, string districtCode, string townName)
        {
            kladrDataSet ds = new kladrDataSet();
            try
            {
                townName = new StringBuilder(townName.Trim().ToUpper()).Append("%").ToString();
                kladrDataSetTableAdapters.kladrTableAdapter kladr = new kladrDataSetTableAdapters.kladrTableAdapter();
                kladr.Connection = new SqlConnection(SqlConnectionString);
                kladr.FillTownByRegionDistrictName(ds.kladr, regionCode, districtCode, townName);
            }
            catch (Exception)
            {
            }
            return ds;
        }

        public addressItem FindTownAddress(string regionCode, string districtCode, string townName)
        {
            kladrDataSet ds = FindTown(regionCode, districtCode, townName);
            addressItem result = FindAddressTemplate(ds);
            return result;
        }

        private kladrDataSet FindCity(string regionCode, string districtCode, string cityName)
        {
            kladrDataSet ds = new kladrDataSet();
            try
            {
                cityName = new StringBuilder(cityName.Trim().ToUpper()).Append("%").ToString();
                kladrDataSetTableAdapters.kladrTableAdapter kladr = new kladrDataSetTableAdapters.kladrTableAdapter();
                kladr.Connection = new SqlConnection(SqlConnectionString);
                kladr.FillCityByRegionDistrictName(ds.kladr, regionCode, districtCode, cityName);
            }
            catch (Exception)
            {
            }
            return ds;
        }

        public addressItem FindCityAddress(string regionCode, string districtCode, string cityName)
        {
            kladrDataSet ds = FindCity(regionCode, districtCode, cityName);
            addressItem result = FindAddressTemplate(ds);
            return result;
        }

        //public void GetRegionByCode(string regionCode, out string name, out string socr)
        //{
        //    name = "";
        //    socr = "";
        //    kladrDataSet ds = new kladrDataSet();
        //    try
        //    {
        //        kladrDataSetTableAdapters.kladrTableAdapter kladr = new kladrDataSetTableAdapters.kladrTableAdapter();
        //        kladr.Connection = new SqlConnection(SqlConnectionString);
        //        kladr.FillRegionByCode(ds.kladr, regionCode);
        //        if (ds.kladr.Rows.Count == 1)
        //        {
        //            kladrDataSet.kladrRow row = (ds.kladr.Rows[0] as kladrDataSet.kladrRow);
        //            name = row.name.Trim();
        //            socr = row.socr.Trim();
        //        }
        //    }
        //    catch (Exception)
        //    {
        //    }
        //}

        public addressItem GetRegionByCodeAddress(string regionCode)
        {
            addressItem result = new addressItem { name = "", code = "", socr = "", ds = null };
            kladrDataSet ds = new kladrDataSet();
            try
            {
                kladrDataSetTableAdapters.kladrTableAdapter kladr = new kladrDataSetTableAdapters.kladrTableAdapter();
                kladr.Connection = new SqlConnection(SqlConnectionString);
                kladr.FillRegionByCode(ds.kladr, regionCode);
                result.ds = ds;
                if (ds.kladr.Rows.Count == 1)
                {
                    kladrDataSet.kladrRow row = (ds.kladr.Rows[0] as kladrDataSet.kladrRow);
                    result.name = row.name.Trim();
                    result.socr = row.socr.Trim();
                    result.code = row.code;
                }
            }
            catch (Exception)
            {
            }
            return result;
        }

        //public void GetDistrictByCode(string regionCode, string districtCode, out string name, out string socr)
        //{
        //    name = "";
        //    socr = "";
        //    if (districtCode == "000") return;
        //    kladrDataSet ds = new kladrDataSet();
        //    try
        //    {
        //        kladrDataSetTableAdapters.kladrTableAdapter kladr = new kladrDataSetTableAdapters.kladrTableAdapter();
        //        kladr.Connection = new SqlConnection(SqlConnectionString);
        //        kladr.FillDistrictByCode(ds.kladr, regionCode, districtCode);
        //        if (ds.kladr.Rows.Count == 1)
        //        {
        //            kladrDataSet.kladrRow row = (ds.kladr.Rows[0] as kladrDataSet.kladrRow);
        //            name = row.name.Trim();
        //            socr = row.socr.Trim().ToUpper();
        //        }
        //    }
        //    catch (Exception)
        //    {
        //    }
        //}

        public addressItem GetDistrictByCodeAddress(string regionCode, string districtCode)
        {
            addressItem address = new addressItem { name = "", socr = "", code = "", ds = null };
            if (districtCode == "000") return address;
            kladrDataSet ds = new kladrDataSet();
            try
            {
                kladrDataSetTableAdapters.kladrTableAdapter kladr = new kladrDataSetTableAdapters.kladrTableAdapter();
                kladr.Connection = new SqlConnection(SqlConnectionString);
                kladr.FillDistrictByCode(ds.kladr, regionCode, districtCode);
                address.ds = ds;
                if (ds.kladr.Rows.Count == 1)
                {
                    kladrDataSet.kladrRow row = (ds.kladr.Rows[0] as kladrDataSet.kladrRow);
                    address.name = row.name.Trim();
                    address.socr = row.socr.Trim().ToUpper();
                    address.code = row.code;
                }
            }
            catch (Exception)
            {
            }
            return address;
        }

        //public void GetCityByCode(string regionCode, string districtCode, string cityCode, out string name, out string socr)
        //{
        //    name = "";
        //    socr = "";
        //    if (cityCode == "000") return;
        //    if (string.IsNullOrEmpty(districtCode)) districtCode = "000";
        //    kladrDataSet ds = new kladrDataSet();
        //    try
        //    {
        //        kladrDataSetTableAdapters.kladrTableAdapter kladr = new kladrDataSetTableAdapters.kladrTableAdapter();
        //        kladr.Connection = new SqlConnection(SqlConnectionString);
        //        kladr.FillCityByCode(ds.kladr, regionCode, districtCode, cityCode);
        //        if (ds.kladr.Rows.Count == 1)
        //        {
        //            kladrDataSet.kladrRow row = (ds.kladr.Rows[0] as kladrDataSet.kladrRow);
        //            name = row.name;
        //            socr = row.socr;
        //        }
        //    }
        //    catch (Exception)
        //    {
        //    }
        //}

        public addressItem GetCityByCodeAddress(string regionCode, string districtCode, string cityCode)
        {
            addressItem address = new addressItem { code="", name = "", socr = "", ds = null};
            if (cityCode == "000") return address;
            if (string.IsNullOrEmpty(districtCode)) districtCode = "000";
            kladrDataSet ds = new kladrDataSet();
            try
            {
                kladrDataSetTableAdapters.kladrTableAdapter kladr = new kladrDataSetTableAdapters.kladrTableAdapter();
                kladr.Connection = new SqlConnection(SqlConnectionString);
                kladr.FillCityByCode(ds.kladr, regionCode, districtCode, cityCode);
                address.ds = ds;
                if (ds.kladr.Rows.Count == 1)
                {
                    kladrDataSet.kladrRow row = (ds.kladr.Rows[0] as kladrDataSet.kladrRow);
                    address.name = row.name.Trim();
                    address.socr = row.socr.Trim().ToUpper();
                    address.code = row.code;
                }
            }
            catch (Exception)
            {
            }
            return address;
        }
        //public void GetTownByCode(string regionCode, string districtCode, string townCode, out string name, out string socr)
        //{
        //    name = "";
        //    socr = "";
        //    if (townCode == "000") return;
        //    if (string.IsNullOrEmpty(districtCode))
        //        districtCode = "000";
        //    kladrDataSet ds = new kladrDataSet();
        //    try
        //    {
        //        kladrDataSetTableAdapters.kladrTableAdapter kladr = new kladrDataSetTableAdapters.kladrTableAdapter();
        //        kladr.Connection = new SqlConnection(SqlConnectionString);
        //        kladr.FillTownByCode(ds.kladr, regionCode, districtCode, townCode);
        //        if (ds.kladr.Rows.Count == 1)
        //        {
        //            kladrDataSet.kladrRow row = (ds.kladr.Rows[0] as kladrDataSet.kladrRow);
        //            name = row.name;
        //            socr = row.socr;
        //        }
        //    }
        //    catch (Exception)
        //    {
        //    }
        //}

        public addressItem GetTownByCodeAddress(string regionCode, string districtCode, string townCode)
        {
            addressItem address = new addressItem { code = "", ds = null, socr = "", name = "" };
            if (townCode == "000") return address;
            if (string.IsNullOrEmpty(districtCode))
                districtCode = "000";
            kladrDataSet ds = new kladrDataSet();
            try
            {
                kladrDataSetTableAdapters.kladrTableAdapter kladr = new kladrDataSetTableAdapters.kladrTableAdapter();
                kladr.Connection = new SqlConnection(SqlConnectionString);
                kladr.FillTownByCode(ds.kladr, regionCode, districtCode, townCode);
                address.ds = ds;
                if (ds.kladr.Rows.Count == 1)
                {
                    kladrDataSet.kladrRow row = (ds.kladr.Rows[0] as kladrDataSet.kladrRow);
                    address.name = row.name.Trim();
                    address.socr = row.socr.Trim().ToUpper();
                    address.code = row.code;
                }
            }
            catch (Exception)
            {
            }
            return address;
        }

        private kladrDataSet FindStreet(string kladrCode, string streetName, string socr)
        {
            kladrDataSet ds = new kladrDataSet();
            try
            {
                streetName = new StringBuilder(streetName.Trim().ToUpper()).Append("%").ToString();
                kladrDataSetTableAdapters.streetTableAdapter street = new kladrDataSetTableAdapters.streetTableAdapter();
                street.Connection = new SqlConnection(SqlConnectionString);
                street.FillStreetByKladrCodeNameSocr(ds.street, kladrCode, streetName, socr);
            }
            catch (Exception)
            {
            }
            return ds;
        }


        private kladrDataSet FindStreet(string kladrCode, string streetName)
        {
            kladrDataSet ds = new kladrDataSet();
            try
            {
                streetName = new StringBuilder(streetName.Trim().ToUpper()).Append("%").ToString();
                kladrDataSetTableAdapters.streetTableAdapter street = new kladrDataSetTableAdapters.streetTableAdapter(); 
                street.Connection = new SqlConnection(SqlConnectionString);
                street.FillStreetByKladrCodeName(ds.street, kladrCode, streetName);
            }
            catch (Exception)
            {
            }
            return ds;
        }

        public addressItem FindStreetAddress(string kladrCode, string streetName, string socr)
        {
            kladrDataSet ds = FindStreet(kladrCode, streetName, socr);
            addressItem result = FindAddressTemplate(ds);
            return result;
        }

        public addressItem FindStreetAddress(string kladrCode, string streetName)
        {
            kladrDataSet ds = FindStreet(kladrCode, streetName);
            addressItem result = FindAddressTemplate(ds);
            return result;
        }
        
    }
}
